package org.lvslb.db.dao;

import org.lvslb.db.pojo.Admin;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.List;

/**
 * Created by MelOnDev1 on 14/05/14.
 */
@Component
public class AdminDao {

    @PersistenceContext
    private EntityManager em;

    @Transactional
    public void persist(Admin admin) {
        em.persist(admin);
    }

    @Transactional
    public void update(Admin admin) {
        em.persist(admin);
    }

    public Admin verify(String userName,String userPassword) {
        TypedQuery<Admin> query = em.createQuery(
                "SELECT g FROM Admin g WHERE g.userName="+userName+" AND g.userPwd="+userPassword, Admin.class);
        return query.getSingleResult();
    }

    public List<Admin> getAllAdmin() {
        TypedQuery<Admin> query = em.createQuery(
                "SELECT g FROM Admin g ORDER BY g.id", Admin.class);
        return query.getResultList();
    }
}
